package br.com.unitunes.dao;

import java.util.List;

import javax.persistence.Query;
import br.com.caelum.vraptor.ioc.Component;
import br.com.unitunes.model.Podcast;

@Component
public class PodcastDAO extends GenericDAO<Podcast>{
	
	@Override
	public Class<Podcast> getModelClass() {
		return Podcast.class;
	}
	
	public List<Podcast> pesquisa(String termo, Long index, Long maxResults, Long autorID) {
		
		String queryStr = "SELECT podcast FROM PODCAST podcast WHERE ( UPPER(podcast.nome) LIKE UPPER(:nome) OR UPPER(podcast.descricao) LIKE UPPER(:descricao) )"; 
		if(autorID != null) {
			queryStr += " AND podcast.autor.id = :autorID";
		}
		
		Query query = getEntityManager().createQuery(queryStr);
		
		query.setParameter("nome", '%' + (termo != null ? termo : "") + '%');
		query.setParameter("descricao", '%' + (termo != null ? termo : "") + '%');

		if(autorID != null) {
			query.setParameter("autorID", autorID);
		}		
		
		return executaQuery(query, maxResults, index);
	}
}
